package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.dao.MenuDao;
import com.itcast.pojo.Menu;
import com.itcast.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> findAll() {
        //获取一级菜单
       List<Menu> menuList = menuDao.findOneMenu();

        return findMenuById(menuList);
    }


    @Override
    public List<Menu> findUserMenu(String username) {
        //获取一级菜单
        List<Menu> menuList = menuDao.findUserMenu(username);
        return findMenuById(menuList);
    }


    public List<Menu> findMenuById(List<Menu> menuList){
        List<Menu> menus = null;
        //遍历
        for (Menu menu : menuList) {
            //获取每一个id
            Integer menuId = menu.getId();
            menus = menuDao.findTMenu(menuId);
            menu.setChildren(menus);
        }
        return menuList;
    }
}
